package com.xdf.xzymanagementsystem.aop;

import com.xdf.xzymanagementsystem.service.base.BaseService;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.springframework.util.Assert;

/**
 * @program: xzy-management-system
 * @description: 参数获取
 * @author: 张柯
 * @create: 2021-04-27 09:52
 **/
public abstract class AbstractArgsGetter extends BaseService {
    protected Object[] getArgsAndValidate(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        Assert.state(ArrayUtils.isNotEmpty(args), "无参数！");
        return args;
    }

    protected Object[] getArgs(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        return args;
    }

    protected Object getParam1(JoinPoint joinPoint) {
        return getParamN(joinPoint, 1);
    }

    protected Object getParam2(JoinPoint joinPoint) {
        return getParamN(joinPoint, 2);
    }

    protected Object getParamN(JoinPoint joinPoint, int n) {
        Object[] args = getArgsAndValidate(joinPoint);
        if (n <= 0) {
            n = 1;
        }
        if (args.length >= n) {
            return args[n - 1];
        }
        throw new RuntimeException("没有第" + n + "个参数！");
    }
}
